home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3150 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: 26 Jan 1996 15:17:44 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan26081744@qcd.lanl.gov>
  8. References: <m0tedv8-0002eqC@sice.nsk.su> <31083426.197A@imsisoft.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Victor Bazarov's message of Thu, 25 Jan 1996 17:53:42 -0800
  13.  
  14. --text follows this line--
  15. In article <31083426.197A@imsisoft.com> Victor Bazarov
  16. <vbazarov@imsisoft.com> writes: 
  17. <snip>
  18.    >  double x, p, q, r;
  19.    > 
  20.    > my compiler (cc on SCO 3.2) calculated following expression:
  21.    > 
  22.    >  x = p * q / r;
  23.    > 
  24.    > as p * (q/r); (I mean, it divided first, than multiplied). That
  25.    > resulted in precision loss.
  26.    > 
  27.    > Is this legal behavior?
  28.  
  29.    According to ISO/IEC 9899, it is implementation defined, I guess.
  30.  
  31. I disagree.
  32.  
  33.  
  34.    On the page 7 the behaviour of the abstract machine is described:
  35.  
  36.    "In the abstract machine, all expressions are evaluated as specified
  37.    by the semantics. An actual implementation need not evaluate part
  38.    of an expression if it can deduce that its value is not used and
  39.    that no needed side effects are produced (including any caused by
  40.    calling a function or accessing a volatile object)."
  41.  
  42.    From the wording it is not immediately clear if compiler is allowed
  43.    to rewrite the expression. But in the example on the page 9 one can
  44.  
  45. I would say it is absolutely clear. The quoted clause does not allow
  46. reordering: nothing else allows it either if there is a difference in
  47. meaning. If it is not allowed by any clause, it is disallowed by last
  48. requirement of 5.1.2.3.
  49.  
  50.    conclude that the expression a + 32760 + b + 5 can be rewritten as
  51.    ((a + 32765) + b) on a machine without exceptions on integer overflow.
  52.  
  53. You are misinterpreting the purpose of the example. This is
  54. illustrating the `as if' rule: the rule at the end of 5.1.2.3 (guess
  55. top of page 8). The reordering is allowed because in a machine without
  56. integer overflow, the effects of a + 32760 + b + 5 are exactly the
  57. same as those of a + 32765 + b. 
  58.  
  59. In the case under discussion, the poster specifically mentioned (by
  60. using the words `precision loss') that there was a noticeable
  61. difference between (p*q)/r and p*(q/r). Given that a difference
  62. exists, the last part (beginning of page 8) of 5.1.2.3 disallows the
  63. reordering. 
  64.  
  65.    So, in any case compiler vendor could say: 'Hey, I'm trying to help
  66.    you here, avoiding overflow from multiplication.', although at the
  67.    closer look, division can produce similar overflow...
  68.  
  69. A compiler vendor can say whatever it likes as long as it does not
  70. claim that the compiler is ANSI conformant.
  71.  
  72.    I few words, put the braces the way you want the expression to be
  73.    evaluated, and complain if compiler does not obey them.
  74.  
  75. Why should a compiler `obey braces'? The braces in (p * q)/r do not do
  76. _anything_ to the meaning of the program.
  77.  
  78. Cheers
  79. Tanmoy
  80. --
  81. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  82. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  83. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  84. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  85. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  86. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  87.